﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PFMGlobals;
using System.Reflection;
using System.Threading;

namespace PFMHandlers
{
    public class PFMHandlerFactory
    {
        private static ReaderWriterLock rwLocker = null;
        protected static void DoInitialization()
        {
            if (rwLocker == null)
                rwLocker = new ReaderWriterLock();
        }
        public static PFMHandler CreateHandler(string sHandler)
        {

            PFMHandler result = null;
            DoInitialization();
            rwLocker.AcquireWriterLock(5000);
            try
            {
                Assembly assembly = Assembly.GetExecutingAssembly();
                foreach (Type t in assembly.GetTypes())
                {
                    if (t.Name.ToUpper().Equals(("PFM" + sHandler + "HandlerFactory").ToUpper()))
                    {
                        result = (PFMHandler)t.InvokeMember("CreateHandler", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, t, new object[] { });
                    }
                }
            }
            finally { rwLocker.ReleaseWriterLock(); }
            return result;

            //if (sHandler.Equals(PFMGlobals.PFMDefinitions.ZIPHANDLER))
            //    result = PFMZipHandlerFactory.CreateHandler();
            //if (sHandler.Equals(PFMGlobals.PFMDefinitions.UNZIPHANDLER))
            //    result = PFMZipHandlerFactory.CreateHandler();

        }

    }
}
